package threads;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.concurrent.CountDownLatch;

import nlp.NamedEntityRecognizer;

public class NER_File implements Runnable{
	
	NamedEntityRecognizer ner;
	String filename;
	String threadname;
	CountDownLatch latch;
	
	public NER_File(String filename_, NamedEntityRecognizer ner_, String threadname_, CountDownLatch latch_){		
		this.filename = filename_;
		this.ner = ner_;
		this.threadname = threadname_;
		this.latch = latch_;
	}
	
    @Override
    public void run() {
    	System.out.println(this.threadname + " processing " + this.filename);
    	
    	//TODO: Logger    	
    	BufferedReader f = null;
    	BufferedWriter f_out = null;
		try {
			f = new BufferedReader(new FileReader( new File(this.filename)));
		} catch (FileNotFoundException e) {
			System.out.println("Error file " + filename + " not found");
			e.printStackTrace();
			System.exit(0);
		}
		try {
			f_out = new BufferedWriter(new FileWriter( new File(this.filename+".tagged")));
		} catch (IOException e1) {
			System.out.println("Cannot create " + filename);
			e1.printStackTrace();
			System.exit(0);
		}
    	String line = null;
    	int count = 0;
    	try {
			while ( ( line = f.readLine() ) != null) {
				String[] article = line.split("\t");
				String category = null;
				String datetime = null;
				String url = null;
				String title = null;
				String text = null;				
				try {
					category = article[0];
					datetime = article[1];
					url = article[2];
					title = article[3];
					text = article[4];
				} catch (Exception e) {
					System.out.println("WARNING: parse error: " + url);
				}
				
				String title_xml = null;
				String text_xml = null;
				
				if (title != null && !title.isEmpty()) title_xml = ner.tagText(title);
				if (text != null && !text.isEmpty()) text_xml = ner.tagText(text);
				
				count++;				
				f_out.write(category + '\t' + datetime + '\t' + url + '\t' + title_xml + '\t' + text_xml + '\n');
				
				if (count % 1000 == 0) {
					System.out.println(":: " + this.threadname + " tagged " + count + "/"+"40138" + " articles");
					f_out.flush();
				}
			}
		} catch (IOException e) {
			System.out.println("Error reading file " + filename);
			e.printStackTrace();
			System.exit(0);
		}
    	try {
			f_out.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
    	latch.countDown();
    	System.out.println(this.threadname + " finished");
    }
}
